using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BEGalleryExtension
{
    [Themeable(true)]
    public class FullPhotoControl : WebControl
    {
        #region Properties

        private int photoID;
        public int PhotoID
        {
            get { return photoID; }
            set { photoID = value; }
        }

        private int directoryID;
        public int DirectoryID
        {
            get { return directoryID; }
            set { directoryID = value; }
        }
	
        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            PhotoInformation pinfo = new PhotoInformation();

            HyperLink link = new HyperLink();
            link.ImageUrl = String.Format(Constants.PhotoHandler, photoID, PhotoSize.ExtraLarge);
            link.Text = pinfo.getPhotoNameFromID(photoID);
            link.NavigateUrl = String.Format(Constants.PhotoHandler, photoID, "original");
            link.Target = "_blank";

            textRenderControl(link, writer);
        }
        
        public void textRenderControl(Control ctl, HtmlTextWriter writer)
        {
            StringWriter sw = new StringWriter();
            ctl.RenderControl(new HtmlTextWriter(sw));
            writer.Write(sw.ToString());
            writer.Write(Environment.NewLine);
        }

        public HyperLink getPreviousLink(QueryString query)
        {
            PhotoInformation pinfo = new PhotoInformation();
            HyperLink link = new HyperLink();
            int prevID = pinfo.getPrevPhoto(directoryID, photoID);
            if (prevID != -1)
                query["PhotoID"] = Convert.ToString(prevID);
            link.Text = "[< Prev]";
            link.NavigateUrl = query.All;
            return link;
        }

        public HyperLink getNextLink(QueryString query)
        {
            PhotoInformation pinfo = new PhotoInformation();
            HyperLink link = new HyperLink();
            int nextID = pinfo.getNextPhoto(directoryID, photoID);
            if (nextID != -1)
                query["PhotoID"] = Convert.ToString(nextID);
            link.Text = "[Next >]";
            link.NavigateUrl = query.All;
            return link;
        }

        public HyperLink getAlbumLink(QueryString query)
        {
            query.RemoveParameter("PhotoID");
            HyperLink link = new HyperLink();
            link.Text = "[Go up to Album]";
            link.NavigateUrl = query.All;
            return link;
        }
    }
}
